<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>esd管理系统</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="/backend/plugins/element-ui/index.css">
    <link rel="stylesheet" href="/backend/plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/backend/styles/list.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>网关列表</h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item>首页</el-breadcrumb-item>
            <el-breadcrumb-item>终端管理</el-breadcrumb-item>
            <el-breadcrumb-item>网关列表</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <el-row>
                <el-col :span="24">
                    <div style="margin-left: 10px;">
                        <el-button type="primary" icon="el-icon-plus" plain class="butT" @click="handleCreate()">添加</el-button>
                    </div>
                </el-col>
            </el-row>
            <el-table
                    size="small"
                    current-row-key="gatewayId"
                    :data="dataList"
                    highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="gatewayName" label="网关名称" align="center"></el-table-column>
                <el-table-column prop="signalChannel" label="网关信道" align="center"></el-table-column>
                <el-table-column prop="serverIp" label="网关ip" align="center"></el-table-column>
                <el-table-column prop="serverPort" label="服务端口" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="120px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="网关名称" prop="gatewayName">
                                    <el-input v-model="formData.gatewayName"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="网关信道" prop="signalChannel">
                                    <el-input type="number"
                                              oninput="value = Number(value) || 0"
                                              v-model.number="formData.signalChannel" />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="网关ip" prop="serverIp">
                                    <el-input v-model="formData.serverIp"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="服务端口" prop="serverPort">
                                    <el-input type="number"
                                              oninput="value = Number(value) || 0"
                                              v-model.number="formData.serverPort" />
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="cancelOpt">取消</el-button>
                        <el-button type="primary" @click="saveOrUpdate()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="/backend/plugins/vue/vue.js"></script>
<script src="/backend/plugins/element-ui/index.js"></script>
<script type="text/javascript" src="/backend/plugins/jquery/jquery.min.js"></script>
<script src="/backend/plugins/axios/axios.min.js"></script>
<script src="/backend/js/request.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            dialogTitle: null ,
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                gatewayName: [{required: true, message: '网关名称为必填项', trigger: 'blur'},
                    {pattern: '[^ \x22]+', message: '网关名称不能为空字符串'},],
                signalChannel: [{required: true, message: '网关信道为必填项', trigger: 'blur'}],
                serverIp: [{required: true, message: '网关ip为必填项', trigger: 'blur'},
                    {pattern: '[^ \x22]+', message: 'IP不能为空字符串'}],
                serverPort: [{required: true, message: '网关服务端口为必填项', trigger: 'blur'}]
            }
        },
        created() {
            this.findAll();
        },
        methods: {
            saveOrUpdate(){
                if(this.dialogTitle === '新增网关信息'){
                    this.handleAdd();
                }else if(this.dialogTitle === '编辑网关信息'){
                    this.handleEdit();
                }
            },
            //编辑
            handleEdit() {
                let arr = this.formData.serverIp.split('.');
                if (
                    arr.length === 4
                    //  &&arr[0] === "192" && arr[1] === "168" && (arr[2] === "4" || arr[2] === "0" || arr[2] === "3" || arr[2] === "1" || arr[2] === "5")
                ){
                    let endNum = +arr[3];
                    if (endNum>1 && endNum<255 && endNum!== 100){
                        //进行表单校验
                        this.$refs['dataAddForm'].validate((valid) => {
                            if (valid) {
                                $axios({
                                    url: '/gateway/updateGateway',
                                    method: 'put',
                                    data: this.formData
                                }).then((res) => {
                                    if (res.code === 0) {
                                        this.$message({ type: 'success', message: res.msg });
                                    } else {
                                        this.$message.error(res.msg);
                                    }
                                }).finally(() => {
                                    this.findAll();
                                    this.dialogFormVisible = false;
                                    this.formData = {}
                                });
                            } else {
                                //表单校验不通过
                                this.$message.error("表单数据校验失败!");
                                return false;
                            }
                        });
                    }else {
                        //IP格式不正确
                        this.$message.error("IP格式不正确，请检查！");
                    }
                }else {
                    //IP格式不正确
                    this.$message.error("IP格式不正确，请检查！");
                }

            },
            //添加
            handleAdd() {
                let arr = this.formData.serverIp.split('.');
                if (
                    arr.length === 4 
                    // &&arr[0] === "192" && arr[1] === "168" && (arr[2] === "4" || arr[2] === "0" || arr[2] === "3" || arr[2] === "1" || arr[2] === "5" )
                ) {
                    let endNum = +arr[3];
                    if (endNum > 1 && endNum < 255 && endNum !== 100) {
                        //进行表单校验
                        this.$refs['dataAddForm'].validate((valid) => {
                            if (valid) {
                                $axios({
                                    url: "/gateway/addGateway",
                                    method: 'post',
                                    data: this.formData
                                }).then((res) => {
                                    if (res.code === 0) {
                                        this.findAll();
                                        this.$message({ type: 'success', message: res.msg });
                                    } else {
                                        this.$message.error(res.msg);
                                    }
                                    this.dialogFormVisible = false;
                                });
                            } else {
                                //校验不通过
                                this.$message.error("数据校验失败，请检查你的输入信息是否正确！");
                                return false;
                            }
                        });
                    }else {
                        //IP格式不正确
                        this.$message.error("IP格式不正确，请检查！");
                    }
                }else {
                    //IP格式不正确
                    this.$message.error("IP格式不正确，请检查！");
                }
            },
            //分页查询
            findAll() {
                axios.get("/gateway/getAll").then((res) => {
                    //解析Controller响应回的数据，为模型数据赋值
                    this.dataList = res.data.data;
                });
            },
            emptySearch() {
                this.pagination.queryString = '' ;
                this.findPage() ;
            },
            // 重置表单
            resetForm() {
                this.formData = {};//重置数据
            },
            // 弹出添加窗口
            handleCreate() {
                //弹出新增窗口
                this.dialogFormVisible = true;
                this.dialogTitle = '新增网关信息' ;
                this.resetForm();
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                this.dialogTitle = '编辑网关信息' ;
                //弹出编辑窗口
                this.dialogFormVisible = true;
                $axios({
                    url: "/gateway/getById/" + row.gatewayId,
                    method: 'get',
                    params: null
                }).then((res) => {
                    if (res.code === 0) {
                        this.formData = res.data;
                    } else {
                        this.$message.error(res.msg);
                    }
                });
            },
            cancelOpt(){
                this.dialogFormVisible = false;
                this.formData = {};
                this.$message("已取消了操作!!");
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //设置最新的页码
                this.pagination.currentPage = currentPage;
                //重新调用findPage方法进行分页查询
                this.findPage();
            },
            handleSizeChange (pageSize) {
                this.pagination.pageSize = pageSize
                this.findPage()
            },
            // 删除
            handleDelete(row) {
                this.$confirm("你确定要删除当前数据吗？", "提示", {//确认框
                    type: 'warning'
                }).then(() => {
                    $axios({
                        url: "/gateway/deleteGateway/" + row.gatewayId,
                        method: 'delete',
                        data: null
                    }).then((res) => {
                        if (res.code === 0) {
                            this.$message({ type: 'success',  message: res.msg });
                            this.findAll();
                        } else {
                            this.$message.error(res.msg);
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '操作已取消'
                    });
                });
            }
        }
    })
</script>
</html>